﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class _Counterboreswithparallelshanksandsolidpilots
    {
        public static void Counterboreswithparallelshanksandsolidpilots(Dictionary<string, Object> data)  //
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
            //作图
            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数

            double d1 = Convert.ToDouble(data["dc"])*scale;
            double d2 = Convert.ToDouble(data["gpd"]) * scale;
            double d3 = Convert.ToDouble(data["dconms"]) * scale;
            double l1 = Convert.ToDouble(data["oal"]) * scale;
            double l2 = Convert.ToDouble(data["lcf"]) * scale;
            double l3 = Convert.ToDouble(data["ls"]) * scale;
            double l4 = Convert.ToDouble(data["gpl"]) * scale;

            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l1 + 6, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 3, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + d2 / 2);
            Vector2 v5 = new Vector2(v4.X + l4 * 6.56 / 8.71, v4.Y);
            Vector2 v6 = new Vector2(v5.X, v3.Y);
            Vector2 v7 = new Vector2(v3.X + l4, v3.Y + d2 * 1.26 / 5.73);
            Vector2 v8 = new Vector2(v7.X, v3.Y + d1 / 2);
            Vector2 v9 = new Vector2(v8.X + l2 * 19.95 / 15.51, v3.Y);
            Vector2 a10 = new Vector2(v9.X - l2 * 1.49 / 15.51, v9.Y + d1 * 2.5 / 9.57);
            Vector2 v10 = new Vector2(v9.X - l2 * 2.85 / 15.51, v3.Y + d1 * 3.56 / 9.57);
            Vector2 v11 = new Vector2(v8.X + l2, v10.Y + d1 * 1.47 / 9.57);
            Vector2 v12 = new Vector2(v8.X + l2 * 3 / 15.51, v8.Y - l2 * 3 / 15.51 * (v8.Y - v9.Y) / (v9.X - v8.X));
            Vector2 v13 = new Vector2(v12.X + l2 * 5.04 / 15.51, v8.Y);
            Vector2 v14 = new Vector2(v11.X + l2 * 0.05 / 15.51, v13.Y);
            Vector2 v15 = new Vector2(v11.X, v9.Y);
            Vector2 v16 = new Vector2(v8.X, v3.Y + l2 * (v8.Y - v9.Y) / (v9.X - v8.X));
            Vector2 v17 = new Vector2(v5.X, v3.Y + d2 * 2.46 / 5.73);
            Vector2 v18 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v19 = new Vector2(v8.X, v18.Y);
            Vector2 v20 = new Vector2(v8.X, v8.Y * -1);
            Vector2 v21 = new Vector2(v14.X, v20.Y);
            Vector2 v22 = new Vector2(0, 0);
            Vector2 v23 = new Vector2(v21.X, v21.Y + d1 * 1.15 / 9.57);
            Vector2 v24 = new Vector2(v3.X, v3.Y - d2 * 0.56 / 5.73);
            Vector2 v25 = new Vector2(v3.X, v19.Y + l4 * (v24.Y - v23.Y) / (l4 + l2));
            Vector2 v26 = new Vector2(0, 0);
            Vector2 v27 = new Vector2(0, 0);
            Vector2 v28 = new Vector2(0, 0);
            Vector2 v29 = new Vector2(0, 0);
            Vector2 v30 = new Vector2(0, 0);
            Vector2 v31 = new Vector2(0, 0);
            Vector2 v32 = new Vector2(0, 0);
            Vector2 v33 = new Vector2(0, 0);
            Vector2 v34 = new Vector2(0, 0);

            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            LwPolyline line2 = new LwPolyline();
            line2.Vertexes.Add(new LwPolylineVertex(v3));
            line2.Vertexes.Add(new LwPolylineVertex(v4));
            line2.Vertexes.Add(new LwPolylineVertex(v5));
            line2.Vertexes.Add(new LwPolylineVertex(v6));
            line2.Vertexes.Add(new LwPolylineVertex(v7));
            line2.Vertexes.Add(new LwPolylineVertex(v8));
            line2.Vertexes.Add(new LwPolylineVertex(v9));
            line2.Vertexes[3].Bulge = Math.Tan(130 * pi / 180 / 4);
            fun.PolyFill(dxf, line2, 0.1, contour);

            Arc c1 = fun.DrawArc3(v11, v14, v10);
            fun.drawBArc(dxf, c1, 0.1, contour);

            Arc c2 = fun.DrawArc3(v11, v13, v12);
            fun.drawBArc(dxf, c2, 0.1, contour);

            Arc c3 = fun.DrawArc3(v9, a10, v10);
            fun.drawBArc(dxf, c3, 0.1, contour);

            LwPolyline line3 = new LwPolyline();
            line3.Vertexes.Add(new LwPolylineVertex(v13));
            line3.Vertexes.Add(new LwPolylineVertex(v14));
            fun.PolyFill(dxf, line3, 0.1, contour);

            LwPolyline line5 = new LwPolyline();
            line5.Vertexes.Add(new LwPolylineVertex(v19));
            line5.Vertexes.Add(new LwPolylineVertex(v25));
            line5.Vertexes.Add(new LwPolylineVertex(v24));
            line5.Vertexes.Add(new LwPolylineVertex(v23));
            line5.Vertexes.Add(new LwPolylineVertex(v7));
            fun.PolyFill(dxf, line5, 0.1, contour);

            LwPolyline line6 = new LwPolyline();
            line6.Vertexes.Add(new LwPolylineVertex(v3));
            line6.Vertexes.Add(new LwPolylineVertex(v18));
            line6.Vertexes.Add(new LwPolylineVertex(v19));
            line6.Vertexes.Add(new LwPolylineVertex(v20));
            line6.Vertexes.Add(new LwPolylineVertex(v21));
            line6.Vertexes.Add(new LwPolylineVertex(v15));
            line6.Vertexes.Add(new LwPolylineVertex(v16));
            line6.Vertexes.Add(new LwPolylineVertex(v17));
            line6.Vertexes[6].Bulge = Math.Tan(17 * pi / 180 / 4);
            fun.PolyFill(dxf, line6, 0.1, contour);

            if (d3 == d1)
            {
                v26 = v14;
                v22 = v21;
                v29 = new Vector2(v3.X + l1 - d3 * 1.03 / 10.86, v26.Y);
                v30 = new Vector2(v3.X + l1, v29.Y - d3 * 1.03 / 10.86);
                v31 = new Vector2(v30.X, v30.Y * -1);
                v32 = new Vector2(v29.X, v22.Y);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(v26));
                line4.Vertexes.Add(new LwPolylineVertex(v29));
                line4.Vertexes.Add(new LwPolylineVertex(v30));
                line4.Vertexes.Add(new LwPolylineVertex(v31));
                line4.Vertexes.Add(new LwPolylineVertex(v32));
                line4.Vertexes.Add(new LwPolylineVertex(v29));
                line4.Vertexes.Add(new LwPolylineVertex(v32));
                line4.Vertexes.Add(new LwPolylineVertex(v22));
                fun.PolyFill(dxf, line4, 0.1, contour);

            }

            if (d3 != d1)
            {
                Vector2 a8 = new Vector2(v8.X, v3.Y + d3 * 3.76 / 10.86);
                Vector2 a9 = new Vector2(a8.X + l1, a8.Y);
                if (v10.Y < d3 * 3.76 / 10.86)
                {
                    v26 = fun.Inters1(a8, a9, c1, 1);
                }

                if (v10.Y > d3 * 3.76 / 10.86)
                {
                    v26 = fun.Inters1(a8, a9, c3, 1);
                }
                v22 = new Vector2(v21.X, v26.Y * -1);
                v27 = new Vector2(v3.X +l1- l3 - (d3 / 2 - v26.Y), v26.Y);
                v28 = new Vector2(v27.X + (d3 / 2 - v26.Y), v3.Y + d3 / 2);
                v29 = new Vector2(v3.X + l1 - d3 * 1.03 / 10.86, v28.Y);
                v30 = new Vector2(v3.X + l1, v29.Y - d3 * 1.03 / 10.86);
                v31 = new Vector2(v30.X, v30.Y * -1);
                v32 = new Vector2(v29.X, v29.Y*-1);
                v33 = new Vector2(v28.X, v32.Y);
                v34 = new Vector2(v27.X, v22.Y);

                LwPolyline line4 = new LwPolyline();
                line4.Vertexes.Add(new LwPolylineVertex(v26));
                line4.Vertexes.Add(new LwPolylineVertex(v27));
                line4.Vertexes.Add(new LwPolylineVertex(v28));
                line4.Vertexes.Add(new LwPolylineVertex(v29));
                line4.Vertexes.Add(new LwPolylineVertex(v30));
                line4.Vertexes.Add(new LwPolylineVertex(v31));
                line4.Vertexes.Add(new LwPolylineVertex(v32));
                line4.Vertexes.Add(new LwPolylineVertex(v29));
                line4.Vertexes.Add(new LwPolylineVertex(v32));
                line4.Vertexes.Add(new LwPolylineVertex(v33));
                line4.Vertexes.Add(new LwPolylineVertex(v28));
                line4.Vertexes.Add(new LwPolylineVertex(v33));
                line4.Vertexes.Add(new LwPolylineVertex(v34));
                line4.Vertexes.Add(new LwPolylineVertex(v22));
                line4.Vertexes[1].Bulge = Math.Tan(90 * pi / 180 / 4);
                line4.Vertexes[11].Bulge = Math.Tan(90 * pi / 180 / 4);
                fun.PolyFill(dxf, line4, 0.1, contour);

            }



            //标注

            double height = 5;
            double arrowsize = 1;
            double exline = 2;



            if (d1 > 8)
            {
                fun.dimrot1(dxf, v4, v18, 90, d1 / 2, 1, height, arrowsize, exline, "GPD=" + d2 / scale, "", "");
                fun.dimrot1(dxf, v8, v20, 90, d1 * 2, 1, height, arrowsize, exline, "DC=" + d1 / scale, "", "");
                fun.dimrot1(dxf, v18, v20, 180, d1 * 1.5, 1, height, arrowsize, exline, "GPL=" + l4 / scale, "", "");
                fun.dimrot1(dxf, v21, v20, 180, d1 * 1.5 + (v20.Y + v21.Y) / 2 - (v18.Y + v20.Y) / 2, 1, height, arrowsize, exline, "LCF=" + l2 / scale, "", "");
                fun.dimrot1(dxf, v18, v31, 180, d1 * 3, 1, height, arrowsize, exline, "OAL=" + l1 / scale, "", "");
                fun.dimrot1(dxf, v29, v32, -90, d1, 1, height, arrowsize, exline, "DCONMS=" + d3 / scale, "", "");
                if (d3 != d1)
                {
                    fun.dimrot1(dxf, v33, v31, 180, d1 * 1.5 - (v20.Y + v18.Y) / 2 + (v33.Y + v31.Y) / 2, 1, height, arrowsize, exline, "LS=" + l3 / scale, "", "");
                }
            }

            if (d1 <= 8)
            {
                fun.dimrot1(dxf, v4, v18, 90, 8 / 2, 1, height, arrowsize, exline, "GPD=" + d2 / scale, "", "");
                fun.dimrot1(dxf, v8, v20, 90, 8 * 2, 1, height, arrowsize, exline, "DC=" + d1 / scale, "", "");
                fun.dimrot1(dxf, v18, v20, 180, 8 * 1.5, 1, height, arrowsize, exline, "GPL=" + l4 / scale, "", "");
                fun.dimrot1(dxf, v21, v20, 180, 8 * 1.5 + (v20.Y + v21.Y) / 2 - (v18.Y + v20.Y) / 2, 1, height, arrowsize, exline, "LCF=" + l2 / scale, "", "");
                fun.dimrot1(dxf, v18, v31, 180, 8 * 3, 1, height, arrowsize, exline, "OAL=" + l1 / scale, "", "");
                fun.dimrot1(dxf, v29, v32, -90, 8, 1, height, arrowsize, exline, "DCONMS=" + d3 / scale, "", "");
                if (d3 != d1)
                {
                    fun.dimrot1(dxf, v33, v31, 180, 8 * 1.5 - (v20.Y + v18.Y) / 2 + (v33.Y + v31.Y) / 2, 1, height, arrowsize, exline, "LS=" + l3 / scale, "", "");
                }
            }

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            //double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);

            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }

            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}

